## https://medium.com/@urban_institute/how-to-create-state-and-county-maps-easily-in-r-577d29300bb2

install.packages('devtools')
install.packages("maps")
install.packages("gpclib")
install.packages("maptools")
install.packages("ggplot2")
install.packages("mapdata")
install.packages("readstata13")
install.packages("urbnmapr")
install.packages("tidyverse")

library(devtools)
devtools::install_github("UrbanInstitute/urbnmapr")
devtools::install_github("UI-Research/urbnthemes")

library(tidyverse)
library(maps)
library(gpclib)
library(maptools) 
library(ggplot2)
library(mapdata)
library(tidyverse)
library(devtools)
library(readstata13)
library(urbnmapr)
library(urbnthemes)

setwd ("/Users/jdarr/Dropbox/Research/SL Media/Replication")

sl_media_fips=read.dta13("SLMedia_FIPS_MMKT_replication.dta")

df_sl <- data.frame(sl_media_fips)

df_sl$county_fips = as.character(df_sl$county_fips)

df_sl %>%
  left_join(counties, by = "county_fips") %>% 
  ggplot(aes(long, lat, group = group, fill = sl_media_2000_dum)) +
  geom_polygon(color = NA) +
  scale_fill_gradient(guide=FALSE) +
  theme(legend.position="none") +
  geom_polygon(data = states, mapping = aes(long, lat, group = group),
               fill = NA, color = "#ffffff") +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  theme_urbn_map()

ggsave("SL_Media_2000.png")


df_sl %>%
  left_join(counties, by = "county_fips") %>% 
  ggplot(aes(long, lat, group = group, fill = sl_media_2004_dum)) +
  geom_polygon(color = NA) +
  scale_fill_gradient(guide=FALSE) +
  theme(legend.position="none") +
  geom_polygon(data = states, mapping = aes(long, lat, group = group),
               fill = NA, color = "#ffffff") +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  theme_urbn_map()

ggsave("SL_Media_2004.png")


df_sl %>%
  left_join(counties, by = "county_fips") %>% 
  ggplot(aes(long, lat, group = group, fill = sl_media_2008_dum)) +
  geom_polygon(color = NA) +
  scale_fill_gradient(guide=FALSE) +
  theme(legend.position="none") +
  geom_polygon(data = states, mapping = aes(long, lat, group = group),
               fill = NA, color = "#ffffff") +
  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
  theme_urbn_map()

ggsave("SL_Media_2008.png")


